package com.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.Result;
import com.demo.pojo.dto.ClazzListDTO;
import com.demo.pojo.entity.Clazz;
import com.demo.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级控制器
 */
@RestController
@RequestMapping("/clazz")
public class ClazzController {
    
    @Autowired
    private ClazzService clazzService;
    
    /**
     * 分页查询班级列表
     * @param pageNum 当前页
     * @param pageSize 每页大小
     * @param clazzName 班级名称（可选）
     * @param grade 年级（可选）
     * @param dept 院系（可选）
     * @return 分页结果
     */
    @GetMapping("/listPage")
    public Result<IPage<ClazzListDTO>> getClazzList(
            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "clazzName", required = false) String clazzName,
            @RequestParam(name = "grade", required = false) String grade,
            @RequestParam(name = "dept", required = false) String dept) {
        
        Page<Clazz> page = new Page<>(pageNum, pageSize);
        IPage<ClazzListDTO> result = clazzService.getClazzList(page, clazzName, grade, dept);
        
        return Result.success(result);
    }
    
    /**
     * 根据ID查询班级
     * @param id 班级ID
     * @return 班级信息
     */
    @GetMapping("/select")
    public Result<Clazz> getClazzById(@RequestParam Long id) {
        Clazz clazz = clazzService.getClazzById(id);
        if (clazz == null) {
            return Result.error("班级不存在");
        }
        return Result.success(clazz);
    }
    
    /**
     * 根据班级编号查询班级
     * @param clazzNo 班级编号
     * @return 班级信息
     */
    @GetMapping("/no/{clazzNo}")
    public Result<Clazz> getClazzByNo(@PathVariable Long clazzNo) {
        Clazz clazz = clazzService.getClazzByNo(clazzNo);
        if (clazz == null) {
            return Result.error("班级不存在");
        }
        return Result.success(clazz);
    }
    
    /**
     * 添加班级
     * @param clazz 班级信息
     * @return 操作结果
     */
    @PostMapping("/save")
    public Result<String> addClazz(@RequestBody Clazz clazz) {
        try {
            boolean success = clazzService.addClazz(clazz);
            if (success) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新班级信息
     * @param clazz 班级信息
     * @return 操作结果
     */
    @PutMapping("/update")
    public Result<String> updateClazz(@RequestBody Clazz clazz) {
        try {
            boolean success = clazzService.updateClazz(clazz);
            if (success) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除班级
     * @param id 班级ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Result<String> deleteClazz(@RequestParam Long id) {
        boolean success = clazzService.deleteClazz(id);
        if (success) {
            return Result.success("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }
    
    /**
     * 批量删除班级
     * @param ids 班级ID列表
     * @return 操作结果
     */
    @DeleteMapping("/batch")
    public Result<String> deleteClazzes(@RequestBody List<Long> ids) {
        boolean success = clazzService.deleteClazzes(ids);
        if (success) {
            return Result.success("批量删除成功");
        } else {
            return Result.error("批量删除失败");
        }
    }
}

